class MyProcessor extends AudioWorkletProcessor {
    constructor() {
        super();
        this.port.onmessage = evt => {
            console.log('Received message:', evt.data);
        };
    }

    process(inputs, outputs, parameters) {
        const output = outputs[0]; // 输出音频缓冲区
        const input = inputs[0];   // 输入音频缓冲区（可选）
        // console.log('Received:', inputs, outputs);

        // 如果有输入，拷贝或处理
        if (input && input.length > 0) {
            for (let channel = 0; channel < output.length; channel++) {
                const inputChannel = input[channel];
                const outputChannel = output[channel];
                for (let i = 0; i < outputChannel.length; i++) {
                    // 简单示例：直接复制输入到输出（原声透传）
                    outputChannel[i] = inputChannel ? inputChannel[i] : 0;
                }
            }
        } else {
            // 没有输入时输出静音
            for (let channel = 0; channel < output.length; channel++) {
                const outputChannel = output[channel];
                for (let i = 0; i < outputChannel.length; i++) {
                    outputChannel[i] = 0;
                }
            }
        }

        return true; // 返回true表示继续处理，false停止
    }
}

registerProcessor('my-processor', MyProcessor);